package com.share.admin.web;

import com.share.admin.model.db.Dictionary;
import com.share.admin.model.db.Menu;
import com.share.admin.model.vo.DictionaryAddOrUpdateReq;
import com.share.admin.model.vo.DictionaryPageReq;
import com.share.admin.model.vo.MenuAddOrUpdateReq;
import com.share.admin.service.IDictionaryService;
import com.share.common.common.Page;
import com.share.common.common.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-09-22
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    @RequestMapping("/listByGCode")
    public Result listByGCode(String gCode) {
        if (StringUtils.isBlank(gCode)) {
            return Result.failure("gCode参数不能为空");
        } else {
            return Result.success(dictionaryService.listByGCode(gCode));
        }
    }

    @RequestMapping("/getByCode")
    public Result getByCode(String code) {
        if (StringUtils.isBlank(code)) {
            return Result.failure("code参数不能为空");
        } else {
            return Result.success(dictionaryService.getByCode(code));
        }
    }

    @RequestMapping("/getByGCodeAndCode")
    public Result getByGCodeAndCode(String gCode, String code) {
        if (StringUtils.isBlank(code)) {
            return Result.failure("code参数不能为空");
        } else {
            return Result.success(dictionaryService.getByGCodeAndCode(gCode, code));
        }
    }

    //返回整个字典列表
    @RequestMapping("/getAllGCode")
    public Result getAllGCode() {
        return Result.success(dictionaryService.getAllGCode());
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pageList")
    public Result resultPageList(DictionaryPageReq req) {
        Page page = dictionaryService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(DictionaryAddOrUpdateReq req) {
        Dictionary record = new Dictionary();
        BeanUtils.copyProperties(req, record);
        return Result.bool(dictionaryService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(DictionaryAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        Dictionary record = new Dictionary();
        BeanUtils.copyProperties(req, record);
        return Result.bool(dictionaryService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(dictionaryService.removeById(id));
    }
}
